package com.example.xianji.widgets;

import com.example.xianji.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MyButton extends View {
    ButtonState buttonState = ButtonState.close;//����״̬
    Bitmap bitmap, bitmap1;
    private int cuurrentX;
    private boolean isSliding = false;

    public MyButton(Context context) {
        super(context);
    }


    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setSlideBackgroundResourse(int slideButtonBackground) {
        bitmap = BitmapFactory.decodeResource(getResources(), slideButtonBackground);
    }


    public void setSwitchBackgroundResourse(int switchBackground) {
        bitmap1 = BitmapFactory.decodeResource(getResources(), switchBackground);
    }

    public enum ButtonState {
        open, close
    }

    public void setButtonState(ButtonState State) {
        buttonState = State;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(bitmap1.getWidth(), bitmap1.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap1, 0, 0, null);
        int left = cuurrentX - bitmap.getWidth() / 2;
        if (isSliding) {
            if (left < 0) {
                left = 0;
            } else if (left > (bitmap1.getWidth() - bitmap.getWidth())) {
                left = bitmap1.getWidth() - bitmap.getWidth();

            }
            canvas.drawBitmap(bitmap, left, 0, null);
        } else {
            if (buttonState == ButtonState.open) {
                canvas.drawBitmap(bitmap,
                        bitmap1.getWidth() - bitmap.getWidth(), 0, null);

            } else {
                canvas.drawBitmap(bitmap, 0, 0, null);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        cuurrentX = (int) event.getX();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isSliding = true;
                break;
            case MotionEvent.ACTION_MOVE:
                int centX = bitmap1.getWidth() / 2;
                if (cuurrentX > centX) {
                    setSwitchBackgroundResourse(R.drawable.kaiguan1);
                } else {
                    setSwitchBackgroundResourse(R.drawable.kaiguan);
                }
                break;
            case MotionEvent.ACTION_UP:
                isSliding = false;
                int centX1 = bitmap1.getWidth() / 2;
                if (cuurrentX > centX1) {
                    if (buttonState != ButtonState.open) {
                        buttonState = ButtonState.open;
                        if (listner != null) {
                            listner.OnToggleStateChange(buttonState);
                            setSwitchBackgroundResourse(R.drawable.kaiguan1);
                        }
                    }
                } else {
                    if (buttonState != ButtonState.close) {
                        buttonState = ButtonState.close;
                        if (listner != null) {
                            listner.OnToggleStateChange(buttonState);
                            setSwitchBackgroundResourse(R.drawable.kaiguan);
                        }
                    }
                }
                break;
        }
        postInvalidate();
        return true;
    }

    private OnToggleStateChangeListner listner;
    public interface OnToggleStateChangeListner {
        void OnToggleStateChange(ButtonState buttonState);
    }

    public void setOnToggleStateChangeListner(OnToggleStateChangeListner listner) {
        this.listner = listner;
    }
}
